package com.qf.controller;

import com.qf.api.UserInfoApi;
import com.qf.po.UserInfo;
import com.qf.vo.JsonBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserInfoApiController {

    @Autowired(required = false)
    private UserInfoApi userInfoApi;
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public JsonBean userInfos(){
        try {
            List<UserInfo> userInfos = userInfoApi.userInfos();
            return new JsonBean(200,"success",userInfos);
        }catch (Exception e){
            return new JsonBean(0,"fail",null);
        }
    }
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public JsonBean userInfo(int id){
        try {
            UserInfo userInfo = userInfoApi.userInfo(id);
            return new JsonBean(200,"success",userInfo);
        }catch (Exception e){
            return new JsonBean(0,"fail",null);
        }
    }
    @RequestMapping(value = "/adduser",method = RequestMethod.POST)
    public JsonBean useradd(@RequestBody UserInfo userInfo){
        try{
            UserInfo useradd = userInfoApi.useradd(userInfo);
            return new JsonBean(200,"success",useradd);
        }catch (Exception e){
            return new JsonBean(0,"fail",null);
        }
    }
    @RequestMapping(value = "/updateuser",method = RequestMethod.PUT)
    public JsonBean update(@RequestBody UserInfo userInfo){
        try {
            UserInfo update = userInfoApi.update(userInfo);
            return new JsonBean(200,"success",update);
        }catch (Exception e){
            return new JsonBean(0,"fail",null);
        }
    }
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public JsonBean delete(int id){
        boolean delete = userInfoApi.delete(id);
        if (delete) {
            return new JsonBean(200, "success", delete);
        }
        return new JsonBean(0, "fail", delete);
    }
}
